clear 
use  "data_auctions_outcomes"

**** figure 1
preserve
collapse firstprice negoziata discretcrit, by(year)
lab var negoziata "Discretproc"
la var firstprice "Firstprice"
lab var discretcrit "DiscretCrit"
twoway  (conn firstprice year, lpattern(dash)) (conn negoziata year, lpattern(longdash)) (conn discretcrit year, lpattern(solid)) , xlabel(2000(4)2016)  legend(rows(3)) ytitle(Share Contracts)
gr export  "figure1.pdf", replace
restore 
*** figure 2
 preserve
 gen group = 1 if treat2008 == 1 
 replace group= 2 if treat2011 == 1
 replace group= 3 if importo_iniziale > 1000000
drop if importo_iniziale <150000 & year <2008
drop if importo_iniziale == . 
keep group criminal_firm_group1 year
collapse criminal_firm_group1, by(group year)
tw (connected criminal_firm_group1 year if group==1, lpattern(longdash)) (connected criminal_firm_group1 year if group==2, lpattern(dash)) (connected criminal_firm_group1 year if group==3) , xlabel(2000 2006 2008 2011 2015)  legend(order(1 "150-500k" 2 "500k-1mil" 3 "above 1 mil")) ytitle(Share Investigated Firms Winning) xline(2008 2011)
gr export "figure2.pdf", replace 
restore 
*** figure 3
preserve
gen proc4 = 1 if discretproc == 1 & discretcrit == 0 
replace proc4 = 2 if discretcrit==1&discretproc==0&negoziata==0 
replace proc4 = 3 if negoziata==1  & discretproc == 0  & discretcrit == 0 
collapse criminal_firm_group1, by(year proc4)
la def proc4  1 "DiscretProc, lowN" 2 "DiscretCrit" 3 "DiscretProc, highN" 
la val proc4 proc4 
la var proc4 "Type of Procedure"
bytwoway connected criminal_firm_group1 year, by(proc4) ytitle("Share investigated firms Winning") xlabel(2000(4)2016) aes(color msymbol)
graph export  "figure3.pdf", replace
restore 
*** figure 4

preserve
collapse discretproc_high discretproc, by(year)
lab var discretproc_high "Discretproc, highN"
lab var discretproc "DiscretProc, lowN"
twoway  (conn discretproc_highn year, lpattern(dash)) (conn discretproc year, lpattern(solid)) , xlabel(2000(4)2016)  legend(rows(1)) ytitle(Share Contracts)
gr export  "${basedir}/tex-files/dfp/plots/negproc_year.png", replace
restore 

***** APPENDIX

*** figure A3
** distribution of bidders
twoway (histogram n_imprese_offerenti if discretcrit ==0 & n_imprese <40,  width(2) color(green%30)) ///
       (histogram n_imprese_offerenti if discretcrit ==1 & n_imprese <40,  width(2) fcolor(none) lcolor(black) lpattern(dash)), ///
	   legend(order(1 "Lowest Price" 2 "DiscretCrit" )) xlabel(0(4)40)
graph export "$output/plots/bidders_scoring_hist.pdf", replace

*** figure A4 
gen period = 1 if data_pubblicazione < mdy(7,1,2006) 
replace period = 2 if data_pubblicazione >= mdy(7,1,2006) & data_pubblicazione <= mdy(12,23,2008)
replace period = 3 if data_pubblicazione > mdy(12,23,2008) & data_pubblicazione <= mdy(05,14,2011)
replace period = 4 if data_pubblicazione >  mdy(05,14,2011)

eststo clear
rddensity importo_iniziale if period == 3, all plot c(500000)  genvars(temp) plot_range(250000 750000) p(2) q(3) graph_options(xtitle("Reserve Price") ytitle("Density") legend(off))
tw (rarea temp_cil temp_cir temp_grid), xtitle("Reserve Price") ytitle("Density") legend(off) xline(500000) xlabel(300000(100000)700000) ylabel(, format(%9.2g))
graph export "figurea3a.pdf", replace
drop temp*
rddensity importo_iniziale if period == 4, all plot c(1000000)  genvars(temp) plot_range(750000 1250000) p(2) q(3) graph_options(xtitle("Reserve Price") ytitle("Density") legend(off))
tw (rarea temp_cil temp_cir temp_grid), xtitle("Reserve Price") ytitle("Density") legend(off) xline(1000000) xlabel(800000(100000)1200000) ylabel(, format(%9.2g))
graph export "figurea3b.pdf", replace
forval x = 3/4{
su soglia`x'
 rdplot criminal_firm_group1 importo_iniziale if  period == `x' & importo_iniziale<1500000, p(2) c(`r(mean)')  lowerend(`low') upperend(`high') title("")
 graph export "figurea3_`x'.pdf", replace
}


